<?php
namespace Users\Controller;

use Zend\Form\Form;
use Zend\Stdlib\ResponseInterface as Response;
use Zend\Stdlib\Parameters;
use Zend\Authentication\AuthenticationService;
use SanAuthWithDbSaveHandler\Storage\IdentityManagerInterface;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Session\Container;
use Zend\View\Model\JsonModel;
use ZfcUser\Model\TuserTable;
use ZfcUser\Model\Tuser;
use Zend\Cache\StorageFactory;
use ScnSocialAuth\Mapper\UserProviderInterface;
use Users\Model\Rates;
use Users\Model\RatesTable;
use Users\Model\Payments;
use Users\Model\PaymentsTable;
use Corporate\Model\Corporateusers;
use Corporate\Model\CorporateusersTable;

class UsersController extends AbstractActionController
{	
	protected $paymentsTable;
	protected $ratesTable;
	protected $navigation;
	protected $otput;
	protected $corporateusersTable;
	protected $adminTable;
	//protected $user_info;
		/*        user start            */
	
	public function headerAction($params)
    {
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$basePath = $baseUrlArr['basePath'];
		$uploadPath = $baseUrlArr['uploadPath'];
		$paypalUrl = $baseUrlArr['paypalUrl'];
		$paypalid = $baseUrlArr['paypalid'];
		return $this->layout()->setVariable(
			"headerarray",array(
				'baseUrl' 		=> 	$baseUrl,
				'basePath'		=>	$basePath,
				'uploadPath'	=>	$uploadPath,
				'paypalUrl'		=>	$paypalUrl,
				'paypalid'		=>	$paypalid,
			)
		);
	}
	public function indexAction()
    {
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$paypalUrl = $baseUrlArr['paypalUrl'];
		$paypalid = $baseUrlArr['paypalid'];
		$result= new ViewModel(array(
				"paypalUrl" 	 =>	$paypalUrl,
				"paypalid"		 =>	$paypalid,
			));
		return $result;
    }
	//html index pages
	
	public function virtualNumbersAction()
    {
	
	}
	public function sipTrunkingAction()
    {
	
	}
	public function hostedPbxAction()
    {
	
	}
	public function voipPlansAction()
    {
	
	}
	public function successAction()
    {
	
	}
	public function rechargeAction()
    {
	
	}
	public function contactFormAction()
    {
		global $ContactSubject;
		global $ContactMessage;
		if($_POST){
		$fname=$_POST['fname'];
		$email=$_POST['email'];
		$Phonenumber=$_POST['Phonenumber'];
		$msg=$_POST['msg'];
		$ContactMessage = str_replace("<NAME>","$fname", $ContactMessage);
		$ContactMessage = str_replace("<EMAIl>","$email", $ContactMessage);
		$ContactMessage = str_replace("<NUMBER>","$Phonenumber", $ContactMessage);
		$ContactMessage = str_replace("<MESSAGE>","$msg", $ContactMessage);
			include('public/PHPMailer_5.2.4/sendmail.php');	
			if(sendMail('kemamurali@gmail.com',$ContactSubject,$ContactMessage)){
				$result = new ViewModel(array(
					'output'  	=>  'success',
				));
				return 	$result;
			}
		}
	}
	public function registerAction()
    {
		$SESSION = new Container('user');
	  if($SESSION['loginNum2']=='' && $SESSION['uname']==''){
		//user Registration
		if($_POST && count($_POST)==8){
			$fname=$_POST['fname'];
			$lname=$_POST['lname'];
			$email=$_POST['email'];
			$pnum1=$_POST['pnum1'];
			$pass=$_POST['pass'];
			
			preg_match_all('!\d+!', $_POST['tag'], $matches);
			$Tname=$fname.' '.$lname;
			$Tnum=$matches[0][0].$pnum1;
			
			//create a customer
			$url='http://176.9.51.8:8080/external/server/CreateCustomer';
			$totalData='{"account":"'.$Tnum.'","name":"'.$Tname.'","feeRateGroup":"2","validTime":"24","memo":"memo","infoCustomerAdditional":{"email":"'.$email.'"}}';
			
			$chlead = curl_init();
			curl_setopt($chlead, CURLOPT_URL, $url);
			curl_setopt($chlead, CURLOPT_USERAGENT, 'SugarConnector/1.4');
			curl_setopt($chlead, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($totalData)));
			curl_setopt($chlead, CURLOPT_VERBOSE, 1);
			curl_setopt($chlead, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($chlead, CURLOPT_CUSTOMREQUEST, "POST"); 
			curl_setopt($chlead, CURLOPT_POSTFIELDS,$totalData);
			curl_setopt($chlead, CURLOPT_SSL_VERIFYPEER, 0);
			$address = curl_exec($chlead);
			$addresserror = curl_error($chlead);
			$data=json_decode($address);
			$retCode=$data->retCode;
			
			//create a phone
			$url1='http://176.9.51.8:8080/external/server/CreatePhone';
			$totalData1='{"autoCreateAccount":false,"e164":"'.$Tnum.'","account":"'.$Tnum.'","password":"'.$pass.'","callLevel":"4","memo":"memo"}';
			$chlead1 = curl_init();
			curl_setopt($chlead1, CURLOPT_URL, $url1);
			curl_setopt($chlead1, CURLOPT_USERAGENT, 'SugarConnector/1.4');
			curl_setopt($chlead1, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($totalData1)));
			curl_setopt($chlead1, CURLOPT_VERBOSE, 1);
			curl_setopt($chlead1, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($chlead1, CURLOPT_CUSTOMREQUEST, "POST"); 
			curl_setopt($chlead1, CURLOPT_POSTFIELDS,$totalData1);
			curl_setopt($chlead1, CURLOPT_SSL_VERIFYPEER, 0);
			$address1 = curl_exec($chlead1);
			$addresserror1 = curl_error($chlead1);
			$data1=json_decode($address1);
			$retCode1=$data1->retCode;
			if($retCode==0 && $retCode1==0){
				return $this->redirect()->toUrl('login');	
			}else{
				return $this->redirect()->toUrl('register?id=1');	
			}
		}
		
		//corp Registration
		if($_POST && count($_POST)==9){
			$cname=$_POST['cname'];
			$uname=$_POST['uname'];
			$password=$_POST['password'];
			$cadd=$_POST['cadd'];
			$emailadd=$_POST['emailadd'];
			$IP=$_POST['IP'];
			
			preg_match_all('!\d+!', $_POST['tag1'], $matches);
			$mobile=$matches[0][0].$_POST['mobile'];
			
			//create a customer
			$url2='http://176.9.51.8:8080/external/server/CreateCustomer';
			$totalData2='{"account":"'.$uname.'","name":"'.$uname.'","feeRateGroup":"1","memo":"memo","infoCustomerAdditional":{"email":"'.$emailadd.'","address":"'.$cadd.'"}}';
			$chlead2 = curl_init();
			curl_setopt($chlead2, CURLOPT_URL, $url2);
			curl_setopt($chlead2, CURLOPT_USERAGENT, 'SugarConnector/1.4');
			curl_setopt($chlead2, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($totalData2)));
			curl_setopt($chlead2, CURLOPT_VERBOSE, 1);
			curl_setopt($chlead2, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($chlead2, CURLOPT_CUSTOMREQUEST, "POST"); 
			curl_setopt($chlead2, CURLOPT_POSTFIELDS,$totalData2);
			curl_setopt($chlead2, CURLOPT_SSL_VERIFYPEER, 0);
			$address2 = curl_exec($chlead2);
			$addresserror2 = curl_error($chlead2);
			$data2=json_decode($address2);
			$retCode2=$data2->retCode;
			
			//create a CreateGatewayMapping
			$url3='http://176.9.51.8:8080/external/server/CreateGatewayMapping';
			$totalData3='{"account":"'.$uname.'","name":"'.$uname.'","password":"'.$password.'","customerPassword":"'.$password.'","callLevel":4,"capacity":10,"remoteIps":"'.$IP.'","registerType":0,"minProfitPercent":1}';
			//echo $totalData3;exit;
			//print_r($totalData3);exit;
			$chlead3 = curl_init();
			curl_setopt($chlead3, CURLOPT_URL, $url3);
			curl_setopt($chlead3, CURLOPT_USERAGENT, 'SugarConnector/1.4');
			curl_setopt($chlead3, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($totalData3)));
			curl_setopt($chlead3, CURLOPT_VERBOSE, 1);
			curl_setopt($chlead3, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($chlead3, CURLOPT_CUSTOMREQUEST, "POST"); 
			curl_setopt($chlead3, CURLOPT_POSTFIELDS,$totalData3);
			curl_setopt($chlead3, CURLOPT_SSL_VERIFYPEER, 0);
			$address3 = curl_exec($chlead3);
			$addresserror3 = curl_error($chlead3);
			$data3=json_decode($address3);
			$retCode3=$data3->retCode;
			if($retCode2==0 && $retCode3==0){
				$SESSION = new Container('user');	
				$SESSION['cnum']=$mobile;
				return $this->redirect()->toUrl('login');	
			}else{
				return $this->redirect()->toUrl('register?id=0');	
			}
		}
	 }else if($SESSION['loginNum2']!=''){
		return $this->redirect()->toUrl('dashboard');
	 }else{
		return $this->redirect()->toUrl('cdashboard');
	 }
	}
	public function loginAction()
    {
	    $SESSION = new Container('user');
		if($SESSION['loginNum2']=='' && $SESSION['uname']==''){
		if($_POST){
				preg_match_all('!\d+!', $_POST['tag'], $matches);
			//user login
				if(isset($_POST['pnumber1']) && $_POST['pnumber1']!=''){
					$pnumber1=$_POST['pnumber1'];
					$number=$matches[0][0].$_POST['pnumber1'];
					$pass=$_POST['pass'];
					
					$url='http://176.9.51.8:8080/external/server/GetPhone';
					$data='{"e164s":["'.$number.'"],"accounts":["'.$number.'"]}';
					
					$chlead = curl_init();
					curl_setopt($chlead, CURLOPT_URL, $url);
					curl_setopt($chlead, CURLOPT_USERAGENT, 'SugarConnector/1.4');
					curl_setopt($chlead, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data)));
					curl_setopt($chlead, CURLOPT_VERBOSE, 1);
					curl_setopt($chlead, CURLOPT_RETURNTRANSFER, true);
					curl_setopt($chlead, CURLOPT_CUSTOMREQUEST, "POST"); 
					curl_setopt($chlead, CURLOPT_POSTFIELDS,$data);
					curl_setopt($chlead, CURLOPT_SSL_VERIFYPEER, 0);
					$login = curl_exec($chlead);
					$addresserror = curl_error($chlead);
					$data=json_decode($login,true);
					$count=count($data['infoPhones']);
					if(count($_POST)==4 && $count==1){
						if($pass==$data['infoPhones'][0]['password']){
							$SESSION = new Container('user');	
							$SESSION['loginNum2']=$number;
							setcookie('Ruser','',time()-3600);
							setcookie('Rpass','',time()-3600);
							setcookie('Rid','',time()-3600);
							setcookie('Rid1','',time()-3600);
							$_COOKIE['Ruser']='';
							$_COOKIE['Rid']='';
							$_COOKIE['Rpass']='';
							$_COOKIE['Rid1']='';
							return $this->redirect()->toUrl('dashboard');	
						}else{
							return $this->redirect()->toUrl('login?id=2');
						}
					}else if(count($_POST)==5 && $count==1){
						if($pass==$data['infoPhones'][0]['password']){
							$SESSION = new Container('user');	
							$SESSION['loginNum2']=$number;
							$SESSION['logout']=$number;
							$SESSION['pwd']=$pass;
							setcookie("Rid1",$_POST['tag'], time()+3600);
							setcookie("Rid",$_POST['tagValue'], time()+3600);
							setcookie("Ruser",$pnumber1, time()+3600);
							setcookie("Rpass",$pass, time()+3600);
							$_COOKIE['Rid']=$_POST['tagValue'];
							$_COOKIE['Rid1']=$_POST['tag'];
							$_COOKIE['Ruser']=$pnumber1;
							$_COOKIE['Rpass']=$pass;
							return $this->redirect()->toUrl('dashboard');	
						}else{
							return $this->redirect()->toUrl('login?id=2');
						}
					}else{
					 	return $this->redirect()->toUrl('login?id=2');	
					}
				}
			//corporate login
		
			if($_POST['uname']!=''){
				if($_POST['cpass']!=''){
					$uname=$_POST['uname'];
					$cpass=$_POST['cpass'];
					
					$url1='http://176.9.51.8:8080/external/server/GetGatewayMapping';
					$cdata='{"names":["'.$uname.'"]}';
					
					$chlead1 = curl_init();
					curl_setopt($chlead1, CURLOPT_URL, $url1);
					curl_setopt($chlead1, CURLOPT_USERAGENT, 'SugarConnector/1.4');
					curl_setopt($chlead1, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($cdata)));
					curl_setopt($chlead1, CURLOPT_VERBOSE, 1);
					curl_setopt($chlead1, CURLOPT_RETURNTRANSFER, true);
					curl_setopt($chlead1, CURLOPT_CUSTOMREQUEST, "POST"); 
					curl_setopt($chlead1, CURLOPT_POSTFIELDS,$cdata);
					curl_setopt($chlead1, CURLOPT_SSL_VERIFYPEER, 0);
					$clogin = curl_exec($chlead1);
					$addresserror = curl_error($chlead1);
					$cdata=json_decode($clogin,true);
					if(count($cdata['infoGatewayMappings'])!=0){
					    if($cdata['infoGatewayMappings'][0]['name'] == $uname && $cdata['infoGatewayMappings'][0]['customerPassword']== $cpass){
							//get link man
							$url2='http://176.9.51.8:8080/external/server/GetCustomer';
							$data2='{"accounts":["'.$uname.'"]}';
							$chlead2 = curl_init();
							curl_setopt($chlead2, CURLOPT_URL, $url2);
							curl_setopt($chlead2, CURLOPT_USERAGENT, 'SugarConnector/1.4');
							curl_setopt($chlead2, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data2)));
							curl_setopt($chlead2, CURLOPT_VERBOSE, 1);
							curl_setopt($chlead2, CURLOPT_RETURNTRANSFER, true);
							curl_setopt($chlead2, CURLOPT_CUSTOMREQUEST, "POST"); 
							curl_setopt($chlead2, CURLOPT_POSTFIELDS,$data2);
							curl_setopt($chlead2, CURLOPT_SSL_VERIFYPEER, 0);
							$account2 = curl_exec($chlead2);
							$addresserror2 = curl_error($chlead2);
							$data2=json_decode($account2,true);
							$SESSION = new Container('user');
							$name=$data2['infoCustomers'][0]['name'];
							$Cname=$data2['infoCustomers'][0]['infoCustomerAdditional']['linkMan'];
							
							//insert linkman or modifying
							if($Cname==''){
							$url13='http://176.9.51.8:8080/external/server/ModifyCustomer';
							$data13='{"account":"'.$name.'","infoCustomerAdditional":{"linkMan":"'.$name.'"}}';
							$chlead13 = curl_init();
							curl_setopt($chlead13, CURLOPT_URL, $url13);
							curl_setopt($chlead13, CURLOPT_USERAGENT, 'SugarConnector/1.4');
							curl_setopt($chlead13, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data13)));
							curl_setopt($chlead13, CURLOPT_VERBOSE, 1);
							curl_setopt($chlead13, CURLOPT_RETURNTRANSFER, true);
							curl_setopt($chlead13, CURLOPT_CUSTOMREQUEST, "POST"); 
							curl_setopt($chlead13, CURLOPT_POSTFIELDS,$data13);
							curl_setopt($chlead13, CURLOPT_SSL_VERIFYPEER, 0);
							$account13 = curl_exec($chlead13);
							$addresserror13 = curl_error($chlead13);
							$SESSION['cname']=$name;
							}else{
								$SESSION['cname']=$data2['infoCustomers'][0]['infoCustomerAdditional']['linkMan'];
							}
							if(count($_POST)==2){
								$SESSION = new Container('user');	
								$SESSION['uname']=$uname;
								setcookie('Cuser','',time()-3600);
								setcookie('Cpass','',time()-3600);
								setcookie("clogout",$uname, time()+3600);
								$_COOKIE['Cuser']='';
								$_COOKIE['Cpass']='';
								
								return $this->redirect()->toUrl('cdashboard');	
							}else if(count($_POST)==3){
								$SESSION = new Container('user');	
								$SESSION['uname']=$uname;
								$SESSION['cnum']=5151;
								setcookie("Cuser",$uname, time()+3600);
								setcookie("Cpass",$cpass, time()+3600);
								setcookie("clogout",$uname, time()+3600);
								
								$_COOKIE['Cuser']=$uname;
								$_COOKIE['Cpass']=$cpass;
								$_COOKIE['clogout']=$uname;
								return $this->redirect()->toUrl('cdashboard');
							}else{
								return $this->redirect()->toUrl('login?id=3');	
							}
						}else{
						return $this->redirect()->toUrl('login?id=3');
						}
					}else{
					return $this->redirect()->toUrl('login?id=3');	
				  }
				}
		    }
		}
	}else if($SESSION['loginNum2']=='' && $SESSION['uname']!=''){
	   return $this->redirect()->toUrl('cdashboard');
	}else{
	  return $this->redirect()->toUrl('dashboard');
	}
	}
	public function logoutAction()
    {
	
	}
    public function dashboardAction()
    { 
		$SESSION = new Container('user');
		if(isset($SESSION['loginNum2']) && $SESSION['loginNum2']!=''){
				//get link man
				$url2='http://176.9.51.8:8080/external/server/GetCustomer';
				$data2='{"accounts":["'.$SESSION['loginNum2'].'"]}';
			
				$chlead2 = curl_init();
				curl_setopt($chlead2, CURLOPT_URL, $url2);
				curl_setopt($chlead2, CURLOPT_USERAGENT, 'SugarConnector/1.4');
				curl_setopt($chlead2, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data2)));
				curl_setopt($chlead2, CURLOPT_VERBOSE, 1);
				curl_setopt($chlead2, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($chlead2, CURLOPT_CUSTOMREQUEST, "POST"); 
				curl_setopt($chlead2, CURLOPT_POSTFIELDS,$data2);
				curl_setopt($chlead2, CURLOPT_SSL_VERIFYPEER, 0);
				$account2 = curl_exec($chlead2);
				$addresserror2 = curl_error($chlead2);
				$data2=json_decode($account2,true);
				$SESSION = new Container('user');
				$name=$data2['infoCustomers'][0]['name'];
			
				if($data2['infoCustomers'][0]['infoCustomerAdditional']['linkMan']==''){
					$url13='http://176.9.51.8:8080/external/server/ModifyCustomer';
					$data13='{"account":"'.$SESSION['loginNum2'].'","infoCustomerAdditional":{"linkMan":"'.$name.'"}}';
					$chlead13 = curl_init();
					curl_setopt($chlead13, CURLOPT_URL, $url13);
					curl_setopt($chlead13, CURLOPT_USERAGENT, 'SugarConnector/1.4');
					curl_setopt($chlead13, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data13)));
					curl_setopt($chlead13, CURLOPT_VERBOSE, 1);
					curl_setopt($chlead13, CURLOPT_RETURNTRANSFER, true);
					curl_setopt($chlead13, CURLOPT_CUSTOMREQUEST, "POST"); 
					curl_setopt($chlead13, CURLOPT_POSTFIELDS,$data13);
					curl_setopt($chlead13, CURLOPT_SSL_VERIFYPEER, 0);
					$account13 = curl_exec($chlead13);
					$addresserror13 = curl_error($chlead13);
				}
				$url12='http://176.9.51.8:8080/external/server/GetCustomer';
				$data12='{"accounts":["'.$SESSION['loginNum2'].'"]}';
			
				$chlead12 = curl_init();
				curl_setopt($chlead12, CURLOPT_URL, $url12);
				curl_setopt($chlead12, CURLOPT_USERAGENT, 'SugarConnector/1.4');
				curl_setopt($chlead12, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data12)));
				curl_setopt($chlead12, CURLOPT_VERBOSE, 1);
				curl_setopt($chlead12, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($chlead12, CURLOPT_CUSTOMREQUEST, "POST"); 
				curl_setopt($chlead12, CURLOPT_POSTFIELDS,$data12);
				curl_setopt($chlead12, CURLOPT_SSL_VERIFYPEER, 0);
				$account12 = curl_exec($chlead12);
				$addresserror12 = curl_error($chlead12);
				$data12=json_decode($account12,true);
				$SESSION = new Container('user');
				$SESSION['UAname']=$data12['infoCustomers'][0]['infoCustomerAdditional']['linkMan'];
				//Get Customer Details
		
				//get customer
				$url1='http://176.9.51.8:8080/external/server/GetCustomer';
				$data1='{"accounts":["'.$SESSION['loginNum2'].'"],"e164s":["'.$SESSION['loginNum2'].'"]}';
				$chlead1 = curl_init();
				curl_setopt($chlead1, CURLOPT_URL, $url1);
				curl_setopt($chlead1, CURLOPT_USERAGENT, 'SugarConnector/1.4');
				curl_setopt($chlead1, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data1)));
				curl_setopt($chlead1, CURLOPT_VERBOSE, 1);
				curl_setopt($chlead1, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($chlead1, CURLOPT_CUSTOMREQUEST, "POST"); 
				curl_setopt($chlead1, CURLOPT_POSTFIELDS,$data1);
				curl_setopt($chlead1, CURLOPT_SSL_VERIFYPEER, 0);
				$account1 = curl_exec($chlead1);
				$addresserror1 = curl_error($chlead1);
				$data1=json_decode($account1,true);
				//echo '<pre>';
				//print_r($data1);exit;
				//GetSuiteOrder user
				$url2='http://176.9.51.8:8080/external/server/GetSuiteOrder';
				$data2='{"ownerName":"'.$SESSION['loginNum2'].'","ownerType":6}';
				$chlead2 = curl_init();
				curl_setopt($chlead2, CURLOPT_URL, $url2);
				curl_setopt($chlead2, CURLOPT_USERAGENT, 'SugarConnector/1.4');
				curl_setopt($chlead2, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data2)));
				curl_setopt($chlead2, CURLOPT_VERBOSE, 1);
				curl_setopt($chlead2, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($chlead2, CURLOPT_CUSTOMREQUEST, "POST"); 
				curl_setopt($chlead2, CURLOPT_POSTFIELDS,$data2);
				curl_setopt($chlead2, CURLOPT_SSL_VERIFYPEER, 0);
				$Suite = curl_exec($chlead2);
				$addresserror2 = curl_error($chlead2);
				$data2=json_decode($Suite,true);
				$SESSION = new Container('user');
				$a=array();
				$b=array();
				$c=array();
				if(count($data2['infoSuiteOrders'])!='' && count($data2['infoSuiteOrders'])!='0'){
					foreach($data2['infoSuiteOrders'] as $data){ 
						array_push($a,$data['suiteId']);
					}
				}
				for($i=0;$i<count($a);$i++){
				//GetSuiteid user
				$url3='http://176.9.51.8:8080/external/server/GetSuite';
				$data3='{"ids":["'.$a[$i].'"]}';
				$chlead3 = curl_init();
				curl_setopt($chlead3, CURLOPT_URL, $url3);
				curl_setopt($chlead3, CURLOPT_USERAGENT, 'SugarConnector/1.4');
				curl_setopt($chlead3, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data3)));
				curl_setopt($chlead3, CURLOPT_VERBOSE, 1);
				curl_setopt($chlead3, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($chlead3, CURLOPT_CUSTOMREQUEST, "POST"); 
				curl_setopt($chlead3, CURLOPT_POSTFIELDS,$data3);
				curl_setopt($chlead3, CURLOPT_SSL_VERIFYPEER, 0);
				$Suitename = curl_exec($chlead3);
				$addresserror3 = curl_error($chlead3);
				$data3=json_decode($Suitename,true);
				
				$SESSION = new Container('user');
				
				array_push($b,$data3['infoSuites'][0]['name']);
				if(isset($data3['infoSuites'][0]['infoGiftTimes'][0]['giftTime'])){
					array_push($c,$data3['infoSuites'][0]['infoGiftTimes'][0]['giftTime']);
				}else{
					array_push($c," ");
				}
			}
			//Latest calls and Graph 
			
			//1days difference
			$date_raw=date("Y-m-d");
			$first_date = strtotime($date_raw);
			$currentDate=date('Ymd', $first_date);
			$second_Wdate = strtotime('+1 day', $first_date);
			$lastWData=date('Ymd', $second_Wdate);
			
			$url4='http://176.9.51.8:8080/external/server/GetCdr';
			$data4='{"accounts":["'.$SESSION['loginNum2'].'"],"beginTime":"'.$currentDate.'","endTime":"'.$lastWData.'"}';
			$chlead4 = curl_init();
			curl_setopt($chlead4, CURLOPT_URL, $url4);
			curl_setopt($chlead4, CURLOPT_USERAGENT, 'SugarConnector/1.4');
			curl_setopt($chlead4, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data4)));
			curl_setopt($chlead4, CURLOPT_VERBOSE, 1);
			curl_setopt($chlead4, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($chlead4, CURLOPT_CUSTOMREQUEST, "POST"); 
			curl_setopt($chlead4, CURLOPT_POSTFIELDS,$data4);
			curl_setopt($chlead4, CURLOPT_SSL_VERIFYPEER, 0);
			$Graph = curl_exec($chlead4);
			$addresserror = curl_error($chlead4);
			$Graph1=json_decode($Graph,true);

			$result= new ViewModel(array(
				"dataDash" 			 =>	$data1,
				"Suitename"		 =>	$b,
				"giftTime"		 =>	$c,
				"graph"          => $Graph1,     
			));
			return $result;
		}else{
			return $this->redirect()->toUrl('login');	
		}		
    }
	public function accountAction()
    {
		$SESSION = new Container('user');
		if(isset($SESSION['loginNum2']) && $SESSION['loginNum2']!=''){
			$data3='';$update='';
			if($_POST){
				$update=1;
				//modify customer
				$url4='http://176.9.51.8:8080/external/server/ModifyCustomer';
				$data4='{"account":"'.$SESSION['loginNum2'].'","infoCustomerAdditional":{"cardType":'.$_POST['memId'].',"cardNumber":"'.$_POST['cardNum'].'","linkMan":"'.$_POST['uname'].'","email":"'.$_POST['Email'].'"}}';
				
				$chlead4 = curl_init();
				curl_setopt($chlead4, CURLOPT_URL, $url4);
				curl_setopt($chlead4, CURLOPT_USERAGENT, 'SugarConnector/1.4');
				curl_setopt($chlead4, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data4)));
				curl_setopt($chlead4, CURLOPT_VERBOSE, 1);
				curl_setopt($chlead4, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($chlead4, CURLOPT_CUSTOMREQUEST, "POST"); 
				curl_setopt($chlead4, CURLOPT_POSTFIELDS,$data4);
				curl_setopt($chlead4, CURLOPT_SSL_VERIFYPEER, 0);
				$account4 = curl_exec($chlead4);
				$addresserror4 = curl_error($chlead4);
				$data4=json_decode($account4,true);
				
				//remaing field i.e password
				$url5='http://176.9.51.8:8080/external/server/ModifyPhone';
				$data5='{"e164":"'.$SESSION['loginNum2'].'","password":"'.$_POST['pass'].'"}';
				$chlead5 = curl_init();
				curl_setopt($chlead5, CURLOPT_URL, $url5);
				curl_setopt($chlead5, CURLOPT_USERAGENT, 'SugarConnector/1.4');
				curl_setopt($chlead5, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data5)));
				curl_setopt($chlead5, CURLOPT_VERBOSE, 1);
				curl_setopt($chlead5, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($chlead5, CURLOPT_CUSTOMREQUEST, "POST"); 
				curl_setopt($chlead5, CURLOPT_POSTFIELDS,$data5);
				curl_setopt($chlead5, CURLOPT_SSL_VERIFYPEER, 0);
				$account5 = curl_exec($chlead5);
				$addresserror5 = curl_error($chlead5);
				$data5=json_decode($account5,true);
			}
				//GetSuiteOrder user
				$url6='http://176.9.51.8:8080/external/server/GetSuiteOrder';
				$data6='{"ownerName":"'.$SESSION['loginNum2'].'","ownerType":6}';
				$chlead6 = curl_init();
				curl_setopt($chlead6, CURLOPT_URL, $url6);
				curl_setopt($chlead6, CURLOPT_USERAGENT, 'SugarConnector/1.4');
				curl_setopt($chlead6, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data6)));
				curl_setopt($chlead6, CURLOPT_VERBOSE, 1);
				curl_setopt($chlead6, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($chlead6, CURLOPT_CUSTOMREQUEST, "POST"); 
				curl_setopt($chlead6, CURLOPT_POSTFIELDS,$data6);
				curl_setopt($chlead6, CURLOPT_SSL_VERIFYPEER, 0);
				$Suite = curl_exec($chlead6);
				$addresserror6 = curl_error($chlead6);
				$data6=json_decode($Suite,true);
				$a=array();
				$b=array();
				$c=array();
				if(count($data6['infoSuiteOrders'])!='' && count($data6['infoSuiteOrders'])!='0'){
					foreach($data6['infoSuiteOrders'] as $data){ 
						array_push($a,$data['suiteId']);
					}
				}
				for($i=0;$i<count($a);$i++){
					$url7='http://176.9.51.8:8080/external/server/GetSuite';
					$data7='{"ids":["'.$a[$i].'"]}';
					$chlead7 = curl_init();
					curl_setopt($chlead7, CURLOPT_URL, $url7);
					curl_setopt($chlead7, CURLOPT_USERAGENT, 'SugarConnector/1.4');
					curl_setopt($chlead7, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data7)));
					curl_setopt($chlead7, CURLOPT_VERBOSE, 1);
					curl_setopt($chlead7, CURLOPT_RETURNTRANSFER, true);
					curl_setopt($chlead7, CURLOPT_CUSTOMREQUEST, "POST"); 
					curl_setopt($chlead7, CURLOPT_POSTFIELDS,$data7);
					curl_setopt($chlead7, CURLOPT_SSL_VERIFYPEER, 0);
					$Suitename = curl_exec($chlead7);
					$addresserror7 = curl_error($chlead7);
					$data7=json_decode($Suitename,true);
					array_push($b,$data7['infoSuites'][0]['name']);
				if(isset($data7['infoSuites'][0]['infoGiftTimes'][0]['giftTime'])){
					array_push($c,$data7['infoSuites'][0]['infoGiftTimes'][0]['giftTime']);
				}else{
					array_push($c," ");
				}
				}
				//get phone
				$url='http://176.9.51.8:8080/external/server/GetPhone';
				$data='{"e164s":["'.$SESSION['loginNum2'].'"],"accounts":["'.$SESSION['loginNum2'].'"]}';
				$chlead = curl_init();
				curl_setopt($chlead, CURLOPT_URL, $url);
				curl_setopt($chlead, CURLOPT_USERAGENT, 'SugarConnector/1.4');
				curl_setopt($chlead, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data)));
				curl_setopt($chlead, CURLOPT_VERBOSE, 1);
				curl_setopt($chlead, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($chlead, CURLOPT_CUSTOMREQUEST, "POST"); 
				curl_setopt($chlead, CURLOPT_POSTFIELDS,$data);
				curl_setopt($chlead, CURLOPT_SSL_VERIFYPEER, 0);
				$account = curl_exec($chlead);
				$addresserror = curl_error($chlead);
				$data=json_decode($account,true);
				$password=$data['infoPhones'][0]['password'];
			
				//get customer
				$url1='http://176.9.51.8:8080/external/server/GetCustomer';
				$data1='{"accounts":["'.$SESSION['loginNum2'].'"],"e164s":["'.$SESSION['loginNum2'].'"]}';
				$chlead1 = curl_init();
				curl_setopt($chlead1, CURLOPT_URL, $url1);
				curl_setopt($chlead1, CURLOPT_USERAGENT, 'SugarConnector/1.4');
				curl_setopt($chlead1, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data1)));
				curl_setopt($chlead1, CURLOPT_VERBOSE, 1);
				curl_setopt($chlead1, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($chlead1, CURLOPT_CUSTOMREQUEST, "POST"); 
				curl_setopt($chlead1, CURLOPT_POSTFIELDS,$data1);
				curl_setopt($chlead1, CURLOPT_SSL_VERIFYPEER, 0);
				$account1 = curl_exec($chlead1);
				$addresserror1 = curl_error($chlead1);
				$data1=json_decode($account1,true);
				$SESSION = new Container('user');
				$SESSION['UAname']=$data1['infoCustomers'][0]['infoCustomerAdditional']['linkMan'];
				$result= new ViewModel(array(
					"data" 			 =>	$data1,
					"password" 		 =>	$password,
					"Suitename"		 =>	$b,
					"giftTime"		 =>	$c,
					"update"		 =>	$update,
				));
				return $result;
		}else{
			return $this->redirect()->toUrl('login');
		}		
    }
	public function callDetailsAction()
    {
		$SESSION = new Container('user');
			$date_raw=date("Y-m-d");
			$first_date = strtotime($date_raw);
			$currentDate=date('Ymd', $first_date);
			$second_date = strtotime('+1 day', $first_date);
			$fromDate = date('Ymd', $second_date);
		if(isset($SESSION['loginNum2']) && $SESSION['loginNum2']!=''){
			$data=0;$pDate='';$pDate1='';$SESSION['datepicker']='';$SESSION['datepicker_to']='';
			$minus='';$plus='';$timezone='';
			if($_POST){
				if(count($_POST)==3){
					if($_POST['timezone']!=0){
							$str=str_split($_POST['timezone']);
							if($str[0]=='-'){
								$z=explode("-",$_POST['timezone']);
								$minus=$z[1]*3600;
								$plus='';
							}else{
								$plus=explode(":",$_POST['timezone']);
								$plus1=$plus[0]*3600;
								$plus2=$plus[1]*60;
								$plus=$plus1+$plus2;
								$minus='';
							}
						$timezone=$_POST['timezone'];
					}else{
						$timezone='';
					}
				}
				if($_POST['datepicker']!='' && $_POST['datepicker_to']!=''){
					$datepicker=explode("/",$_POST['datepicker']);
					$pDate=$datepicker[2].$datepicker[0].$datepicker[1];
					$datepicker1=explode("/",$_POST['datepicker_to']);
					$pDate1=$datepicker1[2].$datepicker1[0].$datepicker1[1];
				}
			}
			if($pDate!=''){
					$data='{"accounts":["'.$SESSION['loginNum2'].'"],"beginTime":"'.$pDate.'","endTime":"'.$pDate1.'"}';
			}else{
				$data='{"accounts":["'.$SESSION['loginNum2'].'"],"beginTime":"'.$currentDate.'","endTime":"'.$fromDate.'"}';
			}
			$url='http://176.9.51.8:8080/external/server/GetCdr';
			$chlead = curl_init();
			curl_setopt($chlead, CURLOPT_URL, $url);
			curl_setopt($chlead, CURLOPT_USERAGENT, 'SugarConnector/1.4');
			curl_setopt($chlead, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data)));
			curl_setopt($chlead, CURLOPT_VERBOSE, 1);
			curl_setopt($chlead, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($chlead, CURLOPT_CUSTOMREQUEST, "POST"); 
			curl_setopt($chlead, CURLOPT_POSTFIELDS,$data);
			curl_setopt($chlead, CURLOPT_SSL_VERIFYPEER, 0);
			$dataTotal = curl_exec($chlead);
			$addresserror = curl_error($chlead);
			$dataFinal=json_decode($dataTotal,true);
			$count=count($dataFinal['infoCdrs']);
			
			$result= new ViewModel(array(
				"data" 			 =>	$dataFinal,
				"plus" 			 =>	$plus,
				"minus" 		 =>	$minus,
				"timezone" 		 =>	$timezone,
				"count" 		 =>	$count,
			));
			return $result;
		}else{
			return $this->redirect()->toUrl('login');
		}
	}
	public function settingsAction()
    {
		$SESSION = new Container('user');
		if(isset($SESSION['loginNum2']) && $SESSION['loginNum2']!=''){
			$data='';$Dtotal='';$Etotal='';$case='false';
			if($_POST){
				//user country id
				if($_POST['snum']!=""){
					preg_match_all('!\d+!', $_POST['tags'], $matches);
					$Dtotal=$_POST['type'].$matches[0][0].$_POST['snum'];
					$case='ture';
				}
				if($_POST['destnum']!=""){
					preg_match_all('!\d+!', $_POST['tags1'], $matches1);
					$Etotal=$matches1[0][0].$_POST['destnum'];
				}
				//destination code id
				$data='{"e164":"'.$SESSION['loginNum2'].'","account":"'.$SESSION['loginNum2'].'","displayNumber":"'.$Etotal.'","infoPhoneValueAdded":{"unconditionalForward":true,"unconditionalForwardE164":{"e164":"'.$Dtotal.'"}}}';
				
				$url='http://176.9.51.8:8080/external/server/ModifyPhone';
				$chlead = curl_init();
				curl_setopt($chlead, CURLOPT_URL, $url);
				curl_setopt($chlead, CURLOPT_USERAGENT, 'SugarConnector/1.4');
				curl_setopt($chlead, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data)));
				curl_setopt($chlead, CURLOPT_VERBOSE, 1);
				curl_setopt($chlead, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($chlead, CURLOPT_CUSTOMREQUEST, "POST"); 
				curl_setopt($chlead, CURLOPT_POSTFIELDS,$data);
				curl_setopt($chlead, CURLOPT_SSL_VERIFYPEER, 0);
				$settings = curl_exec($chlead);
				$addresserror = curl_error($chlead);
				$data1=json_decode($settings);
					if($retCode==0){
						$SESSION = new Container('user');	
						$SESSION['usuccess']=1;
						return $this->redirect()->toUrl('settings?id=2');		
					}else{
						return $this->redirect()->toUrl('settings?id='.$retCode);	
					}	
			}
			$disID='';$disNUM='';$destID='';$destNUM='';
			//get dest number and display number
			$url2='http://176.9.51.8:8080/external/server/GetPhone';
			$data2='{"e164s":["'.$SESSION['loginNum2'].'"]}';
			
			$chlead2 = curl_init();
			curl_setopt($chlead2, CURLOPT_URL, $url2);
			curl_setopt($chlead2, CURLOPT_USERAGENT, 'SugarConnector/1.4');
			curl_setopt($chlead2, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data2)));
			curl_setopt($chlead2, CURLOPT_VERBOSE, 1);
			curl_setopt($chlead2, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($chlead2, CURLOPT_CUSTOMREQUEST, "POST"); 
			curl_setopt($chlead2, CURLOPT_POSTFIELDS,$data2);
			curl_setopt($chlead2, CURLOPT_SSL_VERIFYPEER, 0);
			$setting = curl_exec($chlead2);
			$addresserror = curl_error($chlead2);
			$sdata=json_decode($setting,true);
			$SESSION = new Container('user');
			if(count($sdata['infoPhones'][0]['displayNumber'])==1){
				if($sdata['infoPhones'][0]['displayNumber']!=''){
				$disnum=explode("@",$sdata['infoPhones'][0]['displayNumber']);
				//$disID=$disnum[0];
				//$disNUM=$disnum[1];
				}
			}
			if(isset($sdata['infoPhones'][0]['infoPhoneValueAdded']['infoPhoneValueAddedPeriodForwardings'][0]['forwardE164']['e164'])){
			if(count($sdata['infoPhones'][0]['infoPhoneValueAdded']['infoPhoneValueAddedPeriodForwardings'][0]['forwardE164']['e164'])==1){
				if($sdata['infoPhones'][0]['infoPhoneValueAdded']['infoPhoneValueAddedPeriodForwardings'][0]['forwardE164']['e164']!=''){
				$destnum=$sdata['infoPhones'][0]['infoPhoneValueAdded']['infoPhoneValueAddedPeriodForwardings'][0]['forwardE164']['forwardMethod'];
				$destnum1=$sdata['infoPhones'][0]['infoPhoneValueAdded']['infoPhoneValueAddedPeriodForwardings'][0]['forwardE164']['e164'];
				$destID=$destnum;
				$destNUM=$destnum1;
				}
			}
			}
			$result= new ViewModel(array(
				"disID" 			 =>	$disID,
				"disNUM" 			 =>	$disNUM,
				"destID" 			 =>	$destID,
				"destNUM" 			 =>	$destNUM,
			));
			return $result;
		}else{
			return $this->redirect()->toUrl('login');
		}
    }
	public function paymentsAction()
    {		
			$SESSION = new Container('user');
			$baseUrls = $this->getServiceLocator()->get('config');
			$baseUrlArr = $baseUrls['urls'];
			$paypalUrl = $baseUrlArr['paypalUrl'];
			$paypalid = $baseUrlArr['paypalid'];
			$baseUrl = $baseUrlArr['baseUrl'];
			
			if(isset($SESSION['loginNum2']) && $SESSION['loginNum2']!=''){
				//get current money
				$url='http://176.9.51.8:8080/external/server/GetCustomer';
				$data='{"accounts":["'.$SESSION['loginNum2'].'"]}';
			
				$chlead = curl_init();
				curl_setopt($chlead, CURLOPT_URL, $url);
				curl_setopt($chlead, CURLOPT_USERAGENT, 'SugarConnector/1.4');
				curl_setopt($chlead, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data)));
				curl_setopt($chlead, CURLOPT_VERBOSE, 1);
				curl_setopt($chlead, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($chlead, CURLOPT_CUSTOMREQUEST, "POST"); 
				curl_setopt($chlead, CURLOPT_POSTFIELDS,$data);
				curl_setopt($chlead, CURLOPT_SSL_VERIFYPEER, 0);
				$account = curl_exec($chlead);
				$addresserror = curl_error($chlead);
				$data=json_decode($account,true);
				$result= new ViewModel(array(
					"data" 			=>	$data,
					"paypalUrl" 	=>	$paypalUrl,
					"paypalid" 		=>	$paypalid,
					"baseUrl" 		=>	$baseUrl,
					
				));
				return $result;
			}else{
				return $this->redirect()->toUrl('login');
			}
    }
	
	public function payments3Action()
    {
		$raw_post_data = file_get_contents('php://input');
		$raw_post_array = explode('&', $raw_post_data);
		$myPost = array();
		foreach ($raw_post_array as $keyval) {
		$keyval = explode ('=', $keyval);
		if (count($keyval) == 2)
		$myPost[$keyval[0]] = urldecode($keyval[1]);
		}
		// read the IPN message sent from PayPal and prepend 'cmd=_notify-validate'
		$req = 'cmd=_notify-validate';
		if(function_exists('get_magic_quotes_gpc')) {
		$get_magic_quotes_exists = true;
		}
		foreach ($myPost as $key => $value) {
		if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
		$value = urlencode(stripslashes($value));
		} else {
		$value = urlencode($value);
		}
		$req .= "&$key=$value";
		}
		 
		// Step 2: POST IPN data back to PayPal to validate
		 
		$ch=curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
		curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
		curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
		if(!($res=curl_exec($ch))){
			// error_log("Got " . curl_error($ch) . " when processing IPN data");
			curl_close($ch);
			exit;
		}
		curl_close($ch);
		if(strcmp($res,"VERIFIED")==0){
			//if($_POST){
				$txID=$this->getPaymentsTable()->getTxid($_POST['txn_id']);
				if($txID==''){
					$txid=$this->getPaymentsTable()->addTxid($_POST);
					$money=$_POST['mc_gross']-(4.5/100);
					if($_POST['item_name']=='IPTEL_USER'){
						$data='{"ownerName":"'.$_POST['item_number'].'","ownerType":6,"money":"'.$money.'","memo":"memo"}';
					}else if($_POST['item_name']=='IPTEL_CORP'){
						$data='{"ownerName":"'.$_POST['item_number'].'","ownerType":2,"money":"'.$money.'","memo":"memo"}';
					}
					if($_POST['item_name']=='IPTEL_CORP' || $_POST['item_name']=='IPTEL_USER'){
						$url='http://176.9.51.8:8080/external/server/Pay';
						$chlead = curl_init();
						curl_setopt($chlead, CURLOPT_URL, $url);
						curl_setopt($chlead, CURLOPT_USERAGENT, 'SugarConnector/1.4');
						curl_setopt($chlead, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data)));
						curl_setopt($chlead, CURLOPT_VERBOSE, 1);
						curl_setopt($chlead, CURLOPT_RETURNTRANSFER, true);
						curl_setopt($chlead, CURLOPT_CUSTOMREQUEST, "POST"); 
						curl_setopt($chlead, CURLOPT_POSTFIELDS,$data);
						curl_setopt($chlead, CURLOPT_SSL_VERIFYPEER, 0);
						$account = curl_exec($chlead);
						$addresserror = curl_error($chlead);
					}
				}
		}
	}
	public function payments2Action()
    {
		$SESSION = new Container('user');
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		if($SESSION['loginNum2']!=''){
			return $this->redirect()->toUrl('payment-history');
		}else if($SESSION['uname']!=''){
			return $this->redirect()->toUrl($baseUrl.'/cpayment-history');
		}else{
			return $this->redirect()->toUrl('success');
		}
	}
	public function getPaymentsTable()
    {
        if (!$this->paymentsTable) {
            $sm = $this->getServiceLocator();
            $this->paymentsTable = $sm->get('Users\Model\PaymentsTable');			
        }
        return $this->paymentsTable;
    }
	public function paypakageAction()
    {
		$SESSION = new Container('user');
		 if(isset($SESSION['loginNum2']) && $SESSION['loginNum2']!=''){
			if($_POST){
				$amount=explode("@",$_POST['amount']);
				$url='http://176.9.51.8:8080/external/server/CreateSuiteOrder';
				$data='{"ownerName":"'.$SESSION['loginNum2'].'","ownerType":6,"suiteId":"'.$amount[0].'"}';
				$chlead = curl_init();
				curl_setopt($chlead, CURLOPT_URL, $url);
				curl_setopt($chlead, CURLOPT_USERAGENT, 'SugarConnector/1.4');
				curl_setopt($chlead, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data)));
				curl_setopt($chlead, CURLOPT_VERBOSE, 1);
				curl_setopt($chlead, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($chlead, CURLOPT_CUSTOMREQUEST, "POST"); 
				curl_setopt($chlead, CURLOPT_POSTFIELDS,$data);
				curl_setopt($chlead, CURLOPT_SSL_VERIFYPEER, 0);
				$dataTotal = curl_exec($chlead);
				$addresserror = curl_error($chlead);
				$data=json_decode($dataTotal,true);
				return $this->redirect()->toUrl('dashboard');
			}
		}else if(isset($SESSION['uname']) && $SESSION['uname']!=''){
			if($_POST){
				$amount=explode("@",$_POST['amount']);
				$url='http://176.9.51.8:8080/external/server/CreateSuiteOrder';
				$data='{"ownerName":"'.$SESSION['uname'].'","ownerType":2,"suiteId":"'.$amount[0].'"}';
				$chlead = curl_init();
				curl_setopt($chlead, CURLOPT_URL, $url);
				curl_setopt($chlead, CURLOPT_USERAGENT, 'SugarConnector/1.4');
				curl_setopt($chlead, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data)));
				curl_setopt($chlead, CURLOPT_VERBOSE, 1);
				curl_setopt($chlead, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($chlead, CURLOPT_CUSTOMREQUEST, "POST"); 
				curl_setopt($chlead, CURLOPT_POSTFIELDS,$data);
				curl_setopt($chlead, CURLOPT_SSL_VERIFYPEER, 0);
				$dataTotal = curl_exec($chlead);
				$addresserror = curl_error($chlead);
				$data=json_decode($dataTotal,true);
				return $this->redirect()->toUrl('cdashboard');
			}
		}else{
				return $this->redirect()->toUrl('login');
	    }
	}
	public function packagesHistoryAction()
    {
		$SESSION = new Container('user');
		if(isset($SESSION['loginNum2']) && $SESSION['loginNum2']!=''){
			$url='http://176.9.51.8:8080/external/server/GetSuiteOrder';
			$data='{"ownerName":"'.$SESSION['loginNum2'].'","ownerType":6}';
			
			$chlead = curl_init();
			curl_setopt($chlead, CURLOPT_URL, $url);
			curl_setopt($chlead, CURLOPT_USERAGENT, 'SugarConnector/1.4');
			curl_setopt($chlead, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data)));
			curl_setopt($chlead, CURLOPT_VERBOSE, 1);
			curl_setopt($chlead, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($chlead, CURLOPT_CUSTOMREQUEST, "POST"); 
			curl_setopt($chlead, CURLOPT_POSTFIELDS,$data);
			curl_setopt($chlead, CURLOPT_SSL_VERIFYPEER, 0);
			$dataTotal = curl_exec($chlead);
			$addresserror = curl_error($chlead);
			$dataFinal=json_decode($dataTotal,true);
			$count=count($dataFinal['infoSuiteOrders']);
				$a=array();
				$b=array();
				$c=array();
				$d=array();
			if($count!='0' || $count!=''){
				if(count($dataFinal['infoSuiteOrders'])!='' && count($dataFinal['infoSuiteOrders'])!='0'){
					foreach($dataFinal['infoSuiteOrders'] as $data){ 
						array_push($a,$data['suiteId']);
						array_push($c,$data['availableTime']);
						array_push($d,$data['expireTime']);
					}
				}
				$SESSION['availableTime']=$c;
				$SESSION['expireTime']=$d;
				for($i=0;$i<count($a);$i++){
					$url3='http://176.9.51.8:8080/external/server/GetSuite';
					$data3='{"ids":["'.$a[$i].'"]}';
					$chlead3 = curl_init();
					curl_setopt($chlead3, CURLOPT_URL, $url3);
					curl_setopt($chlead3, CURLOPT_USERAGENT, 'SugarConnector/1.4');
					curl_setopt($chlead3, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data3)));
					curl_setopt($chlead3, CURLOPT_VERBOSE, 1);
					curl_setopt($chlead3, CURLOPT_RETURNTRANSFER, true);
					curl_setopt($chlead3, CURLOPT_CUSTOMREQUEST, "POST"); 
					curl_setopt($chlead3, CURLOPT_POSTFIELDS,$data3);
					curl_setopt($chlead3, CURLOPT_SSL_VERIFYPEER, 0);
					$Suitename = curl_exec($chlead3);
					$addresserror3 = curl_error($chlead3);
					$data3=json_decode($Suitename,true);
					$SESSION = new Container('user');
					array_push($b,$data3['infoSuites'][0]['name']);
				}
			}
			$result= new ViewModel(array(
				"availableTime" 	=>	$c,
				"expireTime" 		=>	$d,
				"Suitename" 		=>	$b,
				"data" 				=>	$dataFinal,
			));
			return $result;
		}else{
			return $this->redirect()->toUrl('login');
		}
    }
	public function paymentHistoryAction()
    {
		$SESSION = new Container('user');
		if(isset($SESSION['loginNum2']) && $SESSION['loginNum2']!=''){
			//5 months differece
			$date_raw=date("Y-m-d");
			$first_date = strtotime($date_raw);
			$end_Date1 = strtotime('+1 day', $first_date);
			$end_Date = date("Ymd", $end_Date1);
			$begin_Date = date("Ymd", strtotime("-3 months"));
			$t='000000';
			$end_Date=$end_Date.$t;
			$begin_Date=$begin_Date.$t;
			
			$url='http://176.9.51.8:8080/external/server/GetPayHistory';
			$data='{"account":"'.$SESSION['loginNum2'].'","beginTime":"'.$begin_Date.'","endTime":"'.$end_Date.'"}';
			$chlead = curl_init();
			curl_setopt($chlead, CURLOPT_URL, $url);
			curl_setopt($chlead, CURLOPT_USERAGENT, 'SugarConnector/1.4');
			curl_setopt($chlead, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data)));
			curl_setopt($chlead, CURLOPT_VERBOSE, 1);
			curl_setopt($chlead, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($chlead, CURLOPT_CUSTOMREQUEST, "POST"); 
			curl_setopt($chlead, CURLOPT_POSTFIELDS,$data);
			curl_setopt($chlead, CURLOPT_SSL_VERIFYPEER, 0);
			$dataTotal = curl_exec($chlead);
			$addresserror = curl_error($chlead);
			$dataFinal=json_decode($dataTotal,true);
			
			$result= new ViewModel(array(
				"payment" 	=>	$dataFinal,
			));
			return $result;
			}else{
			return $this->redirect()->toUrl('login');
		}
    }
	public function usageGraphAction()
    {
		$SESSION = new Container('user');
		if(isset($SESSION['loginNum2']) && $SESSION['loginNum2']!=''){
		
		  $data=0;$pDate='0';$pDate1='0';$actvemenu=1;
			if($_POST){
				if(isset($_POST['Fdate'])){
					$pDate=$_POST['Fdate'];
					$pDate1=$_POST['Ldate'];
					$actvemenu=$_POST['date_1'];
				}
			}
			if($pDate1!='0'){ 
				$data='{"accounts":["'.$SESSION['loginNum2'].'"],"beginTime":"'.$pDate1.'","endTime":"'.$pDate.'"}';
			}else{
				$date_raw=date("Y-m-d");
				$first_date = strtotime($date_raw);
				$currentDate=date('Ymd', $first_date);
				$second_date = strtotime('+1 day', $first_date);
				$fromDate = date('Ymd', $second_date);
				$data='{"accounts":["'.$SESSION['loginNum2'].'"],"beginTime":"'.$currentDate.'","endTime":"'.$fromDate.'"}';
			}
			$url='http://176.9.51.8:8080/external/server/GetCdr';
			$chlead = curl_init();
			curl_setopt($chlead, CURLOPT_URL, $url);
			curl_setopt($chlead, CURLOPT_USERAGENT, 'SugarConnector/1.4');
			curl_setopt($chlead, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data)));
			curl_setopt($chlead, CURLOPT_VERBOSE, 1);
			curl_setopt($chlead, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($chlead, CURLOPT_CUSTOMREQUEST, "POST"); 
			curl_setopt($chlead, CURLOPT_POSTFIELDS,$data);
			curl_setopt($chlead, CURLOPT_SSL_VERIFYPEER, 0);
			$dataTotal = curl_exec($chlead);
			$addresserror = curl_error($chlead);
			$dataFinal=json_decode($dataTotal,true);
			$result= new ViewModel(array(
				"graph1" 	=>	$dataFinal,
				"activedata" => $actvemenu,
			));
			return $result;
		}else{
			return $this->redirect()->toUrl('login');
		}
    }
	public function packages1Action()
    {
        $SESSION = new Container('user');
		if(isset($SESSION['loginNum2']) && $SESSION['loginNum2']!=''){
			//get money
			$url='http://176.9.51.8:8080/external/server/GetCustomer';
			$data='{"accounts":["'.$SESSION['loginNum2'].'"]}';
			$chlead = curl_init();
			curl_setopt($chlead, CURLOPT_URL, $url);
			curl_setopt($chlead, CURLOPT_USERAGENT, 'SugarConnector/1.4');
			curl_setopt($chlead, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data)));
			curl_setopt($chlead, CURLOPT_VERBOSE, 1);
			curl_setopt($chlead, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($chlead, CURLOPT_CUSTOMREQUEST, "POST"); 
			curl_setopt($chlead, CURLOPT_POSTFIELDS,$data);
			curl_setopt($chlead, CURLOPT_SSL_VERIFYPEER, 0);
			$dataTotal = curl_exec($chlead);
			$addresserror = curl_error($chlead);
			$data=json_decode($dataTotal,true);
			//get pakages
			$url1='http://176.9.51.8:8080/external/server/GetSuite';
			$data1='{}';
			$chlead1 = curl_init();
			curl_setopt($chlead1, CURLOPT_URL, $url1);
			curl_setopt($chlead1, CURLOPT_USERAGENT, 'SugarConnector/1.4');
			curl_setopt($chlead1, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data1)));
			curl_setopt($chlead1, CURLOPT_VERBOSE, 1);
			curl_setopt($chlead1, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($chlead1, CURLOPT_CUSTOMREQUEST, "POST"); 
			curl_setopt($chlead1, CURLOPT_POSTFIELDS,$data1);
			curl_setopt($chlead1, CURLOPT_SSL_VERIFYPEER, 0);
			$pakage = curl_exec($chlead1);
			$addresserror = curl_error($chlead1);
			$pakage1=json_decode($pakage,true);
			$result= new ViewModel(array(
				"data" 	=>	$data,
				"pakage" 	=>	$pakage1,
			));
			return $result;
		}else{
			return $this->redirect()->toUrl('login');
		}
    }
	public function forgetPasswordAction()
    {
		global $forgotPasswordSubject;
		global $frogotPasswordMessage;
		if($_POST){
			if($_POST['tag']!='' && $_POST['num']){
				preg_match_all('!\d+!', $_POST['tag'], $matches);
				$number=$matches[0][0].$_POST['num'];
				$url='http://176.9.51.8:8080/external/server/GetPhone';
				$data='{"e164s":["'.$number.'"],"accounts":["'.$number.'"]}';
				
				$chlead = curl_init();
				curl_setopt($chlead, CURLOPT_URL, $url);
				curl_setopt($chlead, CURLOPT_USERAGENT, 'SugarConnector/1.4');
				curl_setopt($chlead, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data)));
				curl_setopt($chlead, CURLOPT_VERBOSE, 1);
				curl_setopt($chlead, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($chlead, CURLOPT_CUSTOMREQUEST, "POST"); 
				curl_setopt($chlead, CURLOPT_POSTFIELDS,$data);
				curl_setopt($chlead, CURLOPT_SSL_VERIFYPEER, 0);
				$login = curl_exec($chlead);
				$addresserror = curl_error($chlead);
				$data=json_decode($login,true);
				$count=count($data['infoPhones']);
				if($count!=0){
					$password=$data['infoPhones'][0]['password'];
					//get email 
					$url1='http://176.9.51.8:8080/external/server/GetCustomer';
					$data1='{"accounts":["'.$number.'"],"e164s":["'.$number.'"]}';
					$chlead1 = curl_init();
					curl_setopt($chlead1, CURLOPT_URL, $url1);
					curl_setopt($chlead1, CURLOPT_USERAGENT, 'SugarConnector/1.4');
					curl_setopt($chlead1, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data1)));
					curl_setopt($chlead1, CURLOPT_VERBOSE, 1);
					curl_setopt($chlead1, CURLOPT_RETURNTRANSFER, true);
					curl_setopt($chlead1, CURLOPT_CUSTOMREQUEST, "POST"); 
					curl_setopt($chlead1, CURLOPT_POSTFIELDS,$data1);
					curl_setopt($chlead1, CURLOPT_SSL_VERIFYPEER, 0);
					$account1 = curl_exec($chlead1);
					$addresserror1 = curl_error($chlead1);
					$data1=json_decode($account1,true);
					$to=$data1['infoCustomers'][0]['infoCustomerAdditional']['email'];
					$frogotPasswordMessage = str_replace("<PASSWORD>","$password", $frogotPasswordMessage);
					include('public/PHPMailer_5.2.4/sendmail.php');	
					if(sendMail($to,$forgotPasswordSubject,$frogotPasswordMessage)){
						$result = new ViewModel(array(
						'output'  	=>  'success',
						));
					}
					return 	$result;
				}else{
					$result = new ViewModel(array(
					'output'  	=>  'notsuccess',
					));
					return 	$result;
				}
			}
		}
    }
	public function cforgetPasswordAction()
    {
		global $forgotCPasswordSubject;
		global $frogotCPasswordMessage;
		if($_POST){
			if($_POST['uname']!=''){
				$url='http://176.9.51.8:8080/external/server/GetGatewayMapping';
				$cdata='{"names":["'.$_POST['uname'].'"]}';
				$chlead = curl_init();
				curl_setopt($chlead, CURLOPT_URL, $url);
				curl_setopt($chlead, CURLOPT_USERAGENT, 'SugarConnector/1.4');
				curl_setopt($chlead, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($cdata)));
				curl_setopt($chlead, CURLOPT_VERBOSE, 1);
				curl_setopt($chlead, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($chlead, CURLOPT_CUSTOMREQUEST, "POST"); 
				curl_setopt($chlead, CURLOPT_POSTFIELDS,$cdata);
				curl_setopt($chlead, CURLOPT_SSL_VERIFYPEER, 0);
				$clogin = curl_exec($chlead);
				$addresserror = curl_error($chlead);
				$cdata=json_decode($clogin,true);
				$count=count($cdata['infoGatewayMappings']);
				if($count!=0){
					$pass=$cdata['infoGatewayMappings'][0]['password'];
					//get email 
					$url1='http://176.9.51.8:8080/external/server/GetCustomer';
					$data1='{"accounts":["'.$_POST['uname'].'"]}';
					$chlead1 = curl_init();
					curl_setopt($chlead1, CURLOPT_URL, $url1);
					curl_setopt($chlead1, CURLOPT_USERAGENT, 'SugarConnector/1.4');
					curl_setopt($chlead1, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data1)));
					curl_setopt($chlead1, CURLOPT_VERBOSE, 1);
					curl_setopt($chlead1, CURLOPT_RETURNTRANSFER, true);
					curl_setopt($chlead1, CURLOPT_CUSTOMREQUEST, "POST"); 
					curl_setopt($chlead1, CURLOPT_POSTFIELDS,$data1);
					curl_setopt($chlead1, CURLOPT_SSL_VERIFYPEER, 0);
					$account1 = curl_exec($chlead1);
					$addresserror1 = curl_error($chlead1);
					$data1=json_decode($account1,true);
					$to=$data1['infoCustomers'][0]['infoCustomerAdditional']['email'];
					$frogotCPasswordMessage = str_replace("<CPASSWORD>","$pass", $frogotCPasswordMessage);
					include('public/PHPMailer_5.2.4/sendmail.php');	
					if(sendMail($to,$forgotCPasswordSubject,$frogotCPasswordMessage)){
						$result = new ViewModel(array(
						'Coutput'  	=>  'success',
						));
					}
					return 	$result;
				}else{
					$result = new ViewModel(array(
					'Coutput'  	=>  'notsuccess',
					));
					return 	$result;
				}
			}
		}
    }
	
	public function callRatesAction()
    {
		$a='A';
		$b='0';
		$navigation=$this->params()->fromRoute('id', 0);
		if($b == $navigation){
			$user_info=$this->getRatesTable()->fetchAll($a);
			$rates_alldata=$this->getRatesTable()->AllRates();
			return new ViewModel(array(
				'user_info' 	=> $user_info,
				'rates_alldata' => $rates_alldata
			));
		}else{
			$user_info=$this->getRatesTable()->fetchAll($navigation);
			$rates_alldata=$this->getRatesTable()->AllRates();
			return new ViewModel(array(
				'user_info'		 => $user_info,
				'rates_alldata'  => $rates_alldata,
			));
		}
	}
	public function getRatesTable()
    {
        if (!$this->ratesTable) {
            $sm = $this->getServiceLocator();
            $this->ratesTable = $sm->get('Users\Model\RatesTable');			
        }
        return $this->ratesTable;
    }
	public function ourAppsAction()
    {
        
    }
	public function dealsAction()
    {
        $SESSION = new Container('user');
			//get pakages
			$url='http://176.9.51.8:8080/external/server/GetSuite';
			$data='{}';
			$chlead = curl_init();
			curl_setopt($chlead, CURLOPT_URL, $url);
			curl_setopt($chlead, CURLOPT_USERAGENT, 'SugarConnector/1.4');
			curl_setopt($chlead, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data)));
			curl_setopt($chlead, CURLOPT_VERBOSE, 1);
			curl_setopt($chlead, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($chlead, CURLOPT_CUSTOMREQUEST, "POST"); 
			curl_setopt($chlead, CURLOPT_POSTFIELDS,$data);
			curl_setopt($chlead, CURLOPT_SSL_VERIFYPEER, 0);
			$pakage = curl_exec($chlead);
			$addresserror = curl_error($chlead);
			$pakage1=json_decode($pakage,true);
			if(isset($SESSION['loginNum2']) && $SESSION['loginNum2']!=''){
				//get money
				$url1='http://176.9.51.8:8080/external/server/GetCustomer';
				$data1='{"accounts":["'.$SESSION['loginNum2'].'"]}';
				$chlead1 = curl_init();
				curl_setopt($chlead1, CURLOPT_URL, $url1);
				curl_setopt($chlead1, CURLOPT_USERAGENT, 'SugarConnector/1.4');
				curl_setopt($chlead1, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data1)));
				curl_setopt($chlead1, CURLOPT_VERBOSE, 1);
				curl_setopt($chlead1, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($chlead1, CURLOPT_CUSTOMREQUEST, "POST"); 
				curl_setopt($chlead1, CURLOPT_POSTFIELDS,$data1);
				curl_setopt($chlead1, CURLOPT_SSL_VERIFYPEER, 0);
				$dataTotal = curl_exec($chlead1);
				$addresserror = curl_error($chlead1);
				$data=json_decode($dataTotal,true);
				$result= new ViewModel(array(
					"pakage" 	=>	$pakage1,
					"rawdata" 	=>	$pakage,
					"data" 	=>	$data,
				));
				return $result;
			}else if(isset($SESSION['uname']) && $SESSION['uname']!=''){
				//get money
				$url1='http://176.9.51.8:8080/external/server/GetCustomer';
				$data1='{"accounts":["'.$SESSION['uname'].'"]}';
				$chlead1 = curl_init();
				curl_setopt($chlead1, CURLOPT_URL, $url1);
				curl_setopt($chlead1, CURLOPT_USERAGENT, 'SugarConnector/1.4');
				curl_setopt($chlead1, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data1)));
				curl_setopt($chlead1, CURLOPT_VERBOSE, 1);
				curl_setopt($chlead1, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($chlead1, CURLOPT_CUSTOMREQUEST, "POST"); 
				curl_setopt($chlead1, CURLOPT_POSTFIELDS,$data1);
				curl_setopt($chlead1, CURLOPT_SSL_VERIFYPEER, 0);
				$dataTotal = curl_exec($chlead1);
				$addresserror = curl_error($chlead1);
				$data=json_decode($dataTotal,true);
				$result= new ViewModel(array(
					"pakage" 	=>	$pakage1,
					"rawdata" 	=>	$pakage,
					"data" 	=>	$data,
				));
				return $result;
			}else{
				$result= new ViewModel(array(
					"pakage" 	=>	$pakage1,
					"rawdata" 	=>	$pakage,
				));
				return $result;
			}
	}
	public function phonebookAction()
	{
		
	}
	public function phonebookaddUserAction()
	{
		
	}
}